/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-02-05
*/
package com.rzico.basics.service;

import com.alibaba.fastjson.JSON;
import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.*;
import com.rzico.basics.mapper.CouponCodeMapper;
import com.rzico.basics.mapper.CouponMapper;
import com.rzico.basics.model.Attach;
import com.rzico.exception.CustomException;
import com.rzico.util.BeanUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.CouponCodeGiftMapper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <pre>
 * 提货券转账记录业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class CouponCodeGiftService extends BaseServiceImpl<CouponCodeGift, String> {

    @Autowired
    private CouponCodeGiftMapper couponCodeGiftMapper;

    @Autowired
    private CouponCodeMapper couponCodeMapper;

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private CouponCodeService couponCodeService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private ProductService productService;

    @Autowired
    private DistributionService distributionService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public BaseMapper<CouponCodeGift, String> getMapper() {
        return couponCodeGiftMapper;
    }

    public void save(List<CouponCodeGift> giftList) {

        for (CouponCodeGift gift:giftList) {
            gift.setCreateDate(new Date());
            couponCodeGiftMapper.insertUseGeneratedKeys(gift);
            CouponCode couponCode = couponCodeMapper.selectByPrimaryKey(gift.getCouponCodeId());
            couponCode.setStock(couponCode.getStock()-gift.getQuantity());
            if (couponCode.getStock()<0) {
                throw new CustomException("券余额不足");
            }
            if (couponCode.getStock()==0) {
                couponCode.setIsUsed(true);
            }
            couponCodeMapper.updateByPrimaryKeySelective(couponCode);
        }

    }

    public void cancel(CouponCodeGift gift) {

        gift.setStatus(2);
        couponCodeGiftMapper.updateByPrimaryKeySelective(gift);
        CouponCode couponCode = couponCodeMapper.selectByPrimaryKey(gift.getCouponCodeId());
        couponCode.setStock(couponCode.getStock()+gift.getQuantity());
        if (couponCode.getStock()>0) {
            couponCode.setIsUsed(false);
        }
        couponCodeMapper.updateByPrimaryKeySelective(couponCode);

    }


    public void confirm(CouponCodeGift gift,String sn) {

        gift.setStatus(1);
        couponCodeGiftMapper.updateByPrimaryKeySelective(gift);
        Coupon coupon = couponMapper.selectByPrimaryKey(gift.getCouponId());
        couponCodeService.create(coupon,gift.getReceiverId(),sn,gift.getQuantity());
        Member sendMember = memberService.findById(gift.getMemberId());
        Member receiverMember = memberService.findById(gift.getReceiverId());

        if (receiverMember.getVvip()>sendMember.getVvip()) {
            Product product = productService.selectByPrimaryKey(coupon.getProductId());
            if (product.getDistributionId() != null) {
                Distribution distribution = distributionService.findById(product.getDistributionId());
                if (distribution != null) {
                    BigDecimal rate = distribution.getVWarehouseRate(receiverMember.vipObject());
                    if (rate.compareTo(BigDecimal.ZERO) > 0) {
                        Attach attachQueue = new Attach();
                        attachQueue.setQuantity(product.getPrice().multiply(rate));
                        attachQueue.setId(String.valueOf(gift.getReceiverId()));
                        attachQueue.setCode("0000");
                        attachQueue.setDescription("仓储管理费");
                        Message message = MessageBuilder
                                .withBody(JSON.toJSONString(attachQueue).getBytes())
                                .setContentType(MessageProperties.CONTENT_TYPE_JSON)
                                .build();
                        CorrelationData correlationData = new CorrelationData();
                        correlationData.setId(UUID.randomUUID().toString());
                        rabbitTemplate.convertAndSend("directExchange", attachQueue.getQueue() + ".key", message, correlationData);
                    }
                }
            }
        }

    }

}
